/**
 * Course			:	ISYS2119 - BIS Development 2
 * Assignment		:	03
 * Date due			:	31/08/2012
 * Program 			:	Saigon Bike Rental Catalogue System
 * 
 * Programmer		:	Vuu Ngoc My Linh - s3342015, Tran Thi Van Anh - s3342025
 * 
 * Attributions		:	Lecture slides, Blackboard, Tutorials, Websites (not in reference list document) 
 */
package rmit.ISYS2119.assign03;

/**
 * @author BloomaJohn [blooma.john@rmit.edu.vn]
 * 
 *
 */

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class AccessoryDAO {

	// Constants
	final String COMMENT_CHAR = "#";
	final String DELIMITER = ";";
	final int NUM_TOKEN_PER_LINE = 7;

	// Variables
	private String dataFileName;
	private BufferedReader in;
	private int lineNum;

	/**
	 * @param dataFileName
	 * @throws FileNotFoundException
	 */
	public AccessoryDAO(String dataFileName) throws FileNotFoundException {
		lineNum = 1;

		// Set the datafile name
		this.dataFileName = dataFileName;

		// Instantiate BufferedReader to use a FileReader to read from the given
		// data file name.
		in = new BufferedReader(new FileReader(this.dataFileName));
	}

	/**
	 * Retrieves the next item from the data file.
	 * 
	 * @return the next Item if it exists, else null.
	 * @throws IOException
	 *             read line exception
	 */
	public Accessory next() throws IOException {
		Accessory accessory = new Accessory();
		String line = null;
		StringTokenizer tokenizerBike;
		StringTokenizer tokenizerSpec;
		String specification;

		// Read the next line
		line = in.readLine();

		if (line != null) {

			// Process line
			tokenizerBike = new StringTokenizer(line, DELIMITER);

			if (tokenizerBike.countTokens() != NUM_TOKEN_PER_LINE) {
				throw new IOException(
						"Invalid number of tokens on line number " + lineNum
								+ ".  There were "
								+ tokenizerBike.countTokens()
								+ " tokens (there should be "
								+ NUM_TOKEN_PER_LINE + " tokens).");
			} else {
				int bikeId = Integer.parseInt(tokenizerBike.nextToken());
				String modelName = tokenizerBike.nextToken();
				String description = tokenizerBike.nextToken();
				int engineCapacity = Integer
						.parseInt(tokenizerBike.nextToken());
				int year = Integer.parseInt(tokenizerBike.nextToken());
				String specifications = tokenizerBike.nextToken();
				tokenizerSpec = new StringTokenizer(specifications, ",");

				accessory.setAccessoryId(accessoryId);
				accessory.setAccessoryName(accessoryName);
				accessory.setTypes(types);
				accessory.setSizes(sizes);
				accessory.setColors(colors);
				
//				while (tokenizerSpec.hasMoreTokens()) {
//					specification = tokenizerSpec.nextToken();
//					BikeEngineSpecification spec = new BikeEngineSpecification(
//							specification);
//					accessory.addSpecifications(spec);
//				}

				accessory.setRentalCost(type);

			}

			lineNum++;
		} else {
			accessory = null;
		}

		// At this point, Java bean is either populated with data or is null.
		return accessory;
	}

	/**
	 * Returns the data file name
	 * 
	 * @return the dataFileName
	 */
	public String getDataFileName() {
		return dataFileName;
	}

	/**
	 * Set the data file name.
	 * 
	 * @param dataFileName
	 *            the dataFile to set
	 */
	public void setDataFileName(String dataFileName) {
		this.dataFileName = dataFileName;
	}

	/**
	 * The finalize() method is call when an object is garbage collected. In the
	 * case of filestreams, it is important to always close them.
	 */
	public void finalize() throws IOException {
		// Close the "in" stream if opened.
		if (in != null) {
			in.close();
		}
	}

	/**
	 * Self-test method.
	 * 
	 * @param args
	 */
	public static void main(String[] args) throws Exception {

		Accessory accessory = null;

		while (accessory != null) {
			System.out.println("AccessoryID: " + accessory.getAccessoryId());
			System.out.println("Accessory Name: " + accessory.getAccessoryName());
			System.out.println("Type: " + accessory.getTypes());
			System.out.println("Size: " + accessory.getSizes());
			for (int i = 0; i < accessory.getColors().size(); i++) {
				System.out.println("Colors: "+ accessory.getColors());
			}

//			System.out.println("Engine Capacity: " + accessory.getEngineCapacity());
//			System.out.println("Year: " + accessory.getYear());
			System.out.println("Cost in VND: " + accessory.getRentalCost());

			System.out.println();

		}
	}
}
